package com.cxkj.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import com.cxkj.common.CommonString;
import com.cxkj.dao.GettableMapper;
import com.cxkj.dao.MeentryMapper;
import com.cxkj.dao.SummaryMapper;
import com.cxkj.dto.Gettable;
import com.cxkj.dto.Meentry;
import com.cxkj.dto.Summary;
import com.cxkj.service.GettableService;
import com.cxkj.tools.CreateFileUtil;
import com.cxkj.tools.Page;

@Service("gettableService")
public class GettableServiceImpl implements GettableService{

	@Autowired
	private GettableMapper gettableMapper;
	@Autowired
	private SummaryMapper summaryMapper;
	@Autowired
	private MeentryMapper meentryMapper;
	
	public int insertGet(Gettable gettable,HttpServletRequest request,MultipartFile file) {
		Integer chuce=Integer.valueOf(request.getParameter("chuce"));
		Integer jiece=Integer.valueOf(request.getParameter("jiece"));
		String driverPic = file.getOriginalFilename();
		int add = 0;
		for (int i = chuce; i <= jiece; i++) {
			gettable.setGvolume(String.valueOf(i));
			gettable.setGstatus("1");
			gettable.setGcreatetime(new Date());
			
			/*驾驶员图片字段gdriverpic*/
			if(driverPic.contains("png")||driverPic.contains("PNG")||driverPic.contains("jpg")||driverPic.contains("JPG")
					||driverPic.contains("gif")||driverPic.contains("GIF")){//判断传过来的文件的类型
	    		String  imgfileD = saveImg(file,"png", 0);
	    		if(imgfileD!=null&&!imgfileD.equals("")){
	    			gettable.setGdriverpic(imgfileD);
	    		}
			}
			//存入领取表
			add = gettableMapper.insertSelective(gettable);
			//修改单据领取状态
			Summary sum = new Summary();
			sum.setSvolume(gettable.getGvolume());
			sum.setSdatastate("已领取");
			int updateGet = summaryMapper.updateGet(sum);
			System.out.println("已领取册号:"+gettable.getGvolume());
			//商户表根据册号添加领取单据的商户名
			Meentry me = new Meentry();
			me.setMvolume(gettable.getGvolume());
			me.setMsogoname(gettable.getGsogoname());
			int ad = meentryMapper.addSogoByGet(me);
			System.out.println("修改"+ad+"条meet数据，商户名："+me.getMsogoname());
			if(i==jiece){
				break;
			}
		}
		
		return add;
	}
	
	/**
	 * 图片处理
	 * @param m
	 * @param type
	 * @param index
	 * @return
	 */
	public String saveImg(MultipartFile m,String type, int index) {
		
		File directory = new File("");// 参数为空
		String   filedir=  "";
		 String  returnDir="";
		FileImageOutputStream imageOutput =null;
		
		 try {
			 byte[] bytes = m.getBytes();
			 /*
			  * 思路
			  * 1，创建文件夹
			  * 2，存储图片写入硬盘
			  * 3，返回路径
			  * */
			filedir= directory.getCanonicalPath();
			filedir=filedir.replace("bin","");
			filedir=filedir+"\\webapps\\yingtao";
			String  destDirName=filedir+"\\Img\\";
			Calendar now = Calendar.getInstance();  
			int   month=now.get(Calendar.MONTH)+1;
			String   datePath="\\"+now.get(Calendar.YEAR)+"\\"+month+"\\"+now.get(Calendar.DAY_OF_MONTH)+"\\";  
			CreateFileUtil.createDir(destDirName+datePath);
			UUID uuid = UUID.randomUUID();
			filedir= filedir+"\\Img\\"+datePath+uuid+"_"+index+"."+type;
			imageOutput = new FileImageOutputStream(new File(filedir));
			imageOutput.write(bytes, 0, bytes.length); 
			imageOutput.close();
			returnDir=CommonString.httpHeadName+"/"+CommonString.projectName+"/"+CommonString.imgFileName+"/"+now.get(Calendar.YEAR)+"/"+month+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+uuid+"_"+index+"."+type;
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
		 
		return returnDir;
	}

	public void showGet(HttpServletRequest request, Model model) {
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		List<Gettable> getList = new ArrayList<Gettable>();
		int count = (int)gettableMapper.getCount();
		if(pageNow != null){
			page = new Page(count,Integer.valueOf(pageNow));
			getList = gettableMapper.showGetByCount(page.getStartPos(), page.getPageSize());
		}else{
			page = new Page(count,1);
			getList = gettableMapper.showGetByCount(page.getStartPos(), page.getPageSize());
		}
		model.addAttribute("getList", getList);
		model.addAttribute("page", page);
	}

	public List<Gettable> getTest(Gettable get) {
		Integer chuce = Integer.valueOf(get.getGvolume());
		Integer jiece = Integer.valueOf(get.getGcolumn1());
		List<Gettable> list= null;
		for (int i = chuce; i <= jiece; i++) {
			list = gettableMapper.getTest(String.valueOf(i));
			if(list.size() != 0){
				break;
			}
			if(i==jiece){
				break;
			}
		}
		
		return list;
	}
	
	
	

}
